<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="org.apak.model.*" import="org.apak.control.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Invoice</title>
</head>
<body>
<%
	String serviceOrderID = (String) request.getAttribute("serviceOrderID");
	Boolean disabled = (Boolean) request.getAttribute("disabled");
	Boolean toServiceOrder = (Boolean) request.getAttribute("toServiceOrder");
	Invoice invoice = (Invoice) request.getAttribute("invoice");
	InvoiceStatusType[] invoiceStatusTypeArray = (InvoiceStatusType[]) request.getAttribute("invoiceStatusTypeArray");
	Customer[] customerArray = (Customer[]) request.getAttribute("customerArray");
	InvoiceRow[] invoiceRowArray = (InvoiceRow[]) request.getAttribute("invoiceRowArray");
	InvoiceForm invoiceForm = (InvoiceForm) request.getAttribute("invoiceForm");
	ErrorList errors = (ErrorList) request.getAttribute("errors");
	String success = (String) request.getAttribute("success");
	String[] fields = {"","","","","1","","","",""};
	String[] errorsText = {"", "", "", "", "", "", ""};
	if(errors != null) {
		invoiceForm = (InvoiceForm) errors.getForm();
		fields[0] = invoiceForm.getReceiver_name();
		fields[1] = invoiceForm.getPrice();
		fields[2] = invoiceForm.getReceiver_accounts();
		fields[3] = invoiceForm.getReference_number();
		fields[4] = invoiceForm.getInvoice_status_type_fk();
		fields[5] = invoiceForm.getDescription();
		fields[6] = Integer.toString(invoiceForm.getInvoice());
		fields[7] = invoiceForm.getCustomer_fk();
		errorsText[0] = errors.getError("receiver_name");
	} else if(invoiceForm != null) {
		fields[0] = invoiceForm.getReceiver_name();
		fields[1] = invoiceForm.getPrice();
		fields[2] = invoiceForm.getReceiver_accounts();
		fields[3] = invoiceForm.getReference_number();
		fields[4] = invoiceForm.getInvoice_status_type_fk();
		fields[5] = invoiceForm.getDescription();
		fields[6] = Integer.toString(invoiceForm.getInvoice());
		fields[7] = invoiceForm.getCustomer_fk();
	}  else if(invoice != null) {
		fields[0] = invoice.getReceiver_name();
		fields[1] = Integer.toString(invoice.getPrice_total());
		fields[2] = invoice.getReceiver_accounts();
		fields[3] = invoice.getReference_number();
		fields[4] = Integer.toString(invoice.getInvoice_status_type_fk());
		if(invoice.getDescription() == null)
			fields[5] = "";
		else
			fields[5] = invoice.getDescription();
		fields[6] = Integer.toString(invoice.getInvoice());
		fields[7] = Integer.toString(invoice.getCustomer_fk());
	}
	if(success == null)
		success = "";
	if(disabled == null || !disabled) {
		if(serviceOrderID != null) {
%>
<a href="./serviceOrder?id=<%= serviceOrderID %>">...back to service order</a>
<%	} %>
<h2>Invoice</h2>
<form action="invoice" method="post">
<table>
	<tr>
		<td>Receiver</td>
		<td><input type="text" name="receiver_name" value="<%= fields[0] %>"><%= errorsText[0] %> </td>
	</tr>
	<tr>
		<td>Customer</td>
		<td>
			<select name="customer">
			<%
					if(customerArray != null)
					for(int k = 0; k < customerArray.length; k++) {
						Customer customer = customerArray[k];
						if(fields[7].equals(Integer.toString(customer.getSubject_fk()))) {
			%>
							<option value="<%= customer.getSubject_fk() %>" selected><%= customer.getCustomerName() %></option>
			<%	
						} else {
			%>
							<option value="<%= customer.getSubject_fk() %>"><%= customer.getCustomerName() %></option>
			<%	
						}
					}
			%>
			</select>
		</td>
	</tr>
	<tr>
		<td>Total price</td>
		<td><input type="text" name="price" value="<%= fields[1] %>" readonly></td>
	</tr>
	<tr>
		<td>Accounts</td>
		<td><input type="text" name="accounts" value="<%= fields[2] %>"> </td>
	</tr>
	<tr>
		<td>Reference number</td>
		<td><input type="text" name="reference_number" value="<%= fields[3] %>"> </td>
	</tr>
	<tr>
		<td>Status</td>
		<td>
			<select name="status">
		<%
				if(invoiceStatusTypeArray != null)
				for(int k = 0; k < invoiceStatusTypeArray.length; k++) {
					InvoiceStatusType statusType = invoiceStatusTypeArray[k];
					if(fields[4].equals(Integer.toString(statusType.getInvoice_status_type()))) {
		%>
						<option value="<%= statusType.getInvoice_status_type() %>" selected><%= statusType.getType_name() %></option>
		<%	
					} else {
		%>
						<option value="<%= statusType.getInvoice_status_type() %>"><%= statusType.getType_name() %></option>
		<%	
					}
				}
		%>
		</select>
		</td>
	</tr>
	<tr>
		<td>Description</td>
		<td><textarea name="employeeDescription"><%= fields[5]%></textarea></td>
	</tr>
</table>
___________________________________________________________________________________
<h3>Invoice row list:</h3>
<%
	if(invoiceRowArray != null) {
%>
<table>
<%
		for(int i = 0; i < invoiceRowArray.length; i++) {
%>
	<tr>
		<td>Action description: </td>
		<td>
			<%= invoiceRowArray[i].getAction_part_description() %>
		</td>
	</tr>
	<tr>
		<td>Amount: </td>
		<td>
			<%= invoiceRowArray[i].getAmount() %>
		</td>
	</tr>
	<tr>
		<td>Unit price: </td>
		<td>
			<%= invoiceRowArray[i].getUnit_price() %>
		</td>
	</tr>
	<tr>
		<td>Unit type: </td>
		<td>
			<%= invoiceRowArray[i].getUnit_type() %>
		</td>
	</tr>
	<tr>
		<td>Price total: </td>
		<td>
			<%= invoiceRowArray[i].getPrice_total() %>
		</td>
	</tr>
	<tr>
		<td colspan="2">__________________________________________________________________________</td>
	</tr>
	<%
		}
	}
	%>
</table>
<input type="submit" name="actionButton" value="Save"/> <%= success %>
</form>
<%
	} else {
		if(toServiceOrder != null && serviceOrderID != null && toServiceOrder) {
%>
<a href="./serviceOrder?id=<%= serviceOrderID %>">...back to service order</a>
<%
		} else {
%>
<a href="./serviceOrderMenu">...back to service order menu</a>
<%
		}
%>
<h2>Invoice</h2>
<table>
	<tr>
		<td>Receiver</td>
		<td><input type="text" name="receiver_name" value="<%= fields[0] %>" readonly><%= errorsText[0] %> </td>
	</tr>
	<tr>
		<td>Customer</td>
		<td>
			<select name="customer" disabled>
			<%
					if(customerArray != null)
					for(int k = 0; k < customerArray.length; k++) {
						Customer customer = customerArray[k];
						if(fields[7].equals(Integer.toString(customer.getSubject_fk()))) {
			%>
							<option value="<%= customer.getSubject_fk() %>" selected><%= customer.getCustomerName() %></option>
			<%	
						} else {
			%>
							<option value="<%= customer.getSubject_fk() %>"><%= customer.getCustomerName() %></option>
			<%	
						}
					}
			%>
			</select>
		</td>
	</tr>
	<tr>
		<td>Total price</td>
		<td><input type="text" name="price" value="<%= fields[1] %>" readonly></td>
	</tr>
	<tr>
		<td>Accounts</td>
		<td><input type="text" name="accounts" value="<%= fields[2] %>" readonly> </td>
	</tr>
	<tr>
		<td>Reference number</td>
		<td><input type="text" name="reference_number" value="<%= fields[3] %>" readonly> </td>
	</tr>
	<tr>
		<td>Status</td>
		<td>
			<select name="status" disabled>
		<%
				if(invoiceStatusTypeArray != null)
				for(int k = 0; k < invoiceStatusTypeArray.length; k++) {
					InvoiceStatusType statusType = invoiceStatusTypeArray[k];
					if(fields[4].equals(Integer.toString(statusType.getInvoice_status_type()))) {
		%>
						<option value="<%= statusType.getInvoice_status_type() %>" selected><%= statusType.getType_name() %></option>
		<%	
					} else {
		%>
						<option value="<%= statusType.getInvoice_status_type() %>"><%= statusType.getType_name() %></option>
		<%	
					}
				}
		%>
		</select>
		</td>
	</tr>
	<tr>
		<td>Description</td>
		<td><textarea name="employeeDescription" readonly><%= fields[5]%></textarea></td>
	</tr>
</table>
___________________________________________________________________________________
<h3>Invoice row list:</h3>
<%
	if(invoiceRowArray != null) {
%>
<table>
<%
		for(int i = 0; i < invoiceRowArray.length; i++) {
%>
	<tr>
		<td>Action description: </td>
		<td>
			<%= invoiceRowArray[i].getAction_part_description() %>
		</td>
	</tr>
	<tr>
		<td>Amount: </td>
		<td>
			<%= invoiceRowArray[i].getAmount() %>
		</td>
	</tr>
	<tr>
		<td>Unit price: </td>
		<td>
			<%= invoiceRowArray[i].getUnit_price() %>
		</td>
	</tr>
	<tr>
		<td>Unit type: </td>
		<td>
			<%= invoiceRowArray[i].getUnit_type() %>
		</td>
	</tr>
	<tr>
		<td>Price total: </td>
		<td>
			<%= invoiceRowArray[i].getPrice_total() %>
		</td>
	</tr>
	<tr>
		<td colspan="2">__________________________________________________________________________</td>
	</tr>
	<%
		}
	}
	%>
</table>
<%
	}
%>
</body>
</html>